package com.agile.leetcode.concurrent.threadLocalRandom;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadLocalRandom;

/**
 * ThreadLocalRandomDemo正确的使用
 *
 * @Author:ChenZhangKun
 * @Date: 2021/4/7 14:12
 */
public class ThreadLocalRandomDemo {
    public static void main(String[] args) {
        ThreadLocalRandomDemo threadLocalRandomDemo = new ThreadLocalRandomDemo();
        ExecutorService threadPool = Executors.newFixedThreadPool(5);
        for (int i = 0; i < 5; i++) {

            threadPool.execute(() -> {
                threadLocalRandomDemo.test();
            });
        }

    }

    public void test() {
        // 注意不能讲ThreadLocalRandom作为主线程成员使用，不然会导致seed共享导致随机数一致性问题
        System.out.println(ThreadLocalRandom.current().nextInt());
    }
}
